package com.grechur.wanandroidcompose.widget

import android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.PointerEventPass
import androidx.compose.ui.input.pointer.changedToDownIgnoreConsumed
import androidx.compose.ui.input.pointer.changedToUpIgnoreConsumed
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.input.pointer.positionChangeConsumed
import androidx.compose.ui.unit.dp
import coil.annotation.ExperimentalCoilApi
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.rememberPagerState
import com.grechur.wanandroidcompose.bean.banner.BannerItem
import com.grechur.wanandroidcompose.widget.banner.util.ImageLoader
import kotlinx.coroutines.delay

@ExperimentalCoilApi
@ExperimentalPagerApi
@Composable
fun BannerView(items: List<BannerItem>, timeMillis:Long = 3000L,onBannerClick: (BannerItem) -> Unit) {
    Box(
        Modifier
            .fillMaxWidth()
            .height(160.dp)) {

        val pagerState = rememberPagerState()
        var executeChangePage by remember { mutableStateOf(false) }
        var currentPageIndex = 0

        HorizontalPager(count = items.size,
            state = pagerState,
            modifier = Modifier
                .pointerInput(pagerState.currentPage) {
                    awaitPointerEventScope {
                        while (true) {
                            val event = awaitPointerEvent(PointerEventPass.Initial)
                            val dragEvent = event.changes.firstOrNull()
                            when {
                                dragEvent!!.positionChangeConsumed() -> {
                                    return@awaitPointerEventScope
                                }

                                dragEvent.changedToDownIgnoreConsumed() -> {
                                    //记录下当前的页面索引值
                                    currentPageIndex = pagerState.currentPage
                                }

                                dragEvent.changedToUpIgnoreConsumed() -> {
                                    if (pagerState.targetPage == null) return@awaitPointerEventScope
                                    if (currentPageIndex == pagerState.currentPage && pagerState.pageCount > 1) {
                                        executeChangePage = !executeChangePage
                                    }
                                }
                            }
                        }
                    }
                }
                .clickable {
                    Log.e(
                        "bannerTAG",
                        "点击的banner item:${pagerState.currentPage}  itemUrl:${items[pagerState.currentPage].url}"
                    )
                    onBannerClick(items[pagerState.currentPage])
                }
                .fillMaxSize()) { page ->
//            Image(
//                painter = rememberAsyncImagePainter(items[page].data),
//                modifier = Modifier.fillMaxSize(),
//                contentScale = ContentScale.Crop,
//                contentDescription = null
//            )
            ImageLoader(data = items[page].data)
        }

        //自动轮播
        LaunchedEffect(key1 = executeChangePage, block = {
            Log.e("LaunchedEffect","${pagerState.currentPage}")
            if (pagerState.currentPage >=0 && pagerState.currentPage < items.size -1){
                delay(timeMillis = timeMillis)
                val page = pagerState.currentPage +1
                pagerState.animateScrollToPage(page)
                executeChangePage = !executeChangePage
            }else{
                delay(timeMillis = timeMillis)
                pagerState.animateScrollToPage(0)
                executeChangePage = !executeChangePage
            }
        })

    }
}